package org.xhy.ruleengine.groovy;

import org.xhy.ruleengine.tree.Node;
import org.xhy.ruleengine.utils.BeanUtils;

import java.util.List;

/**
 * @description:
 * @gitee: https://gitee.com/XhyQAQ
 * @copyright: B站: https://space.bilibili.com/152686439
 * @Author: Xhy
 * @CreateTime: 2024-06-23 21:26
 */
public class GroovyClassLoader implements IGroovyClassLoader {

    @Override
    public Class generateClass(List<Node> nodes) {
        String s = "";
        for (Node node : nodes) {
            String expression = parseExpressionWithMap(node.getExpression());
            // true:还有子节点
            if (node.getType()) {

                s += "if(" +expression+"){\n";
                final List<Node> childNodes = node.getChildNodes();
                for (Node childNode : childNodes) {
                    if (childNode.getType()) {
                        s+= generateClass(childNode.getChildNodes());
                    }else {
                        expression = parseExpressionWithMap(childNode.getExpression());
                        if (childNode.getResult().getClass().equals(String.class)){
                            childNode.setResult("\""+childNode.getResult()+"\"");
                        }
                        s+="if(" +expression+"){\n" +
                                "return " + childNode.getResult()+ ";" +
                                "\n}\n";
                    }
                }
                s+="}\n";
            }else {
                if (node.getResult().getClass().equals(String.class)){
                    node.setResult("\""+node.getResult()+"\"");
                }
                s+="if(" +expression+"){\n" +
                        "return " + node.getResult()+  ";"+
                        "\n}\n";
            }
        }

        final String javaSrc = getJavaSrc(s);
        groovy.lang.GroovyClassLoader groovyClassLoader = new groovy.lang.GroovyClassLoader();
        Class<?> clazz = groovyClassLoader.parseClass(javaSrc);
        return clazz;
    }

    private  String getJavaSrc(String expression){

        String s = "package org.xhy.ruleengine.sample;\n" +
                "\n" +
                "import java.util.Map;\n" +
                "import org.xhy.ruleengine.services.RuleEngine;" +
                "import org.xhy.ruleengine.utils.BeanUtils;"+
                "\n" +
                "\n" +
                "public class Groovy implements RuleEngine{\n" +
                "    public Object exec(Object object){\n" +
                "Map map = BeanUtils.convertToMap(object);"+
                "        \n" + expression+
                "        return null;\n" +
                "    }\n" +
                "}";

        return s;
    }



    private String parseExpressionWithMap(String expression){
        // base case
        if (expression.indexOf("{") == -1) {
            return expression;
        }
        // 模板开始位置
        int start = 0;
        // 模板结束位置
        int end = 0;
        char[] chars = expression.toCharArray();
        boolean flag = false;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '{') {
                start = i;
            }
            if (chars[i] == '}') {
                end = i;
                flag = true;
            }
            // 起始位置和终止位置都找到了
            if (flag) {
                String substring = expression.substring(start + 1, end);
                String a = "{" + substring + "}";
                Object value = String.format("map.get(\"%s\")",substring);
                expression = expression.replace(a,value.toString());
                return parseExpressionWithMap(expression);

            }
        }

        return expression;
    }


}
